﻿#region

using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

#endregion

namespace AeroWizard.Design
{
  internal class WizardPageDesigner : ParentControlDesigner
  {
    private static readonly string[] propsToRemove = new[]
                                                       {
                                                         "Anchor", "AutoScrollOffset", "AutoSize", "BackColor",
                                                         "BackgroundImage", "BackgroundImageLayout", "ContextMenuStrip",
                                                         "Cursor", "Dock", "Enabled", "Font",
                                                         "ForeColor", "Location", "Margin", "MaximumSize", "MinimumSize"
                                                         , "Padding", /*"Size",*/ "TabStop", "UseWaitCursor",
                                                         "Visible"
                                                       };

    private WizardPageDesignerActionList actionList;
    //private DesignerVerbCollection verbs;

    public override DesignerActionListCollection ActionLists
    {
      get
      {
        if (actionList == null)
          actionList = new WizardPageDesignerActionList(this);
        return new DesignerActionListCollection(new DesignerActionList[] {actionList});
      }
    }

    public override SelectionRules SelectionRules
    {
      get { return (SelectionRules.Visible | SelectionRules.Locked); }
    }

    protected override bool EnableDragRect
    {
      get { return false; }
    }

    public override bool CanBeParentedTo(IDesigner parentDesigner)
    {
      return ((parentDesigner != null) && (parentDesigner.Component is TabControl));
    }

    public override void Initialize(IComponent component)
    {
      base.Initialize(component);
      var service = GetService(typeof (DesignerActionService)) as DesignerActionService;
      if (service != null)
        service.Remove(component);
    }

    internal void OnDragDropInternal(DragEventArgs de)
    {
      OnDragDrop(de);
    }

    internal void OnDragEnterInternal(DragEventArgs de)
    {
      OnDragEnter(de);
    }

    internal void OnDragLeaveInternal(EventArgs e)
    {
      OnDragLeave(e);
    }

    internal void OnDragOverInternal(DragEventArgs e)
    {
      OnDragOver(e);
    }

    internal void OnGiveFeedbackInternal(GiveFeedbackEventArgs e)
    {
      OnGiveFeedback(e);
    }

    protected override void OnPaintAdornments(PaintEventArgs pe)
    {
      var clientRectangle = Control.ClientRectangle;
      clientRectangle.Width--;
      clientRectangle.Height--;
      ControlPaint.DrawFocusRectangle(pe.Graphics, clientRectangle);
      base.OnPaintAdornments(pe);
    }

    protected override void PreFilterProperties(IDictionary properties)
    {
      base.PreFilterProperties(properties);
      foreach (var p in propsToRemove)
        if (properties.Contains(p))
          properties.Remove(p);
    }

    #region Nested type: WizardPageDesignerActionList

    internal class WizardPageDesignerActionList : DesignerActionList
    {
      private WizardPageDesigner wizardPageDesigner;

      public WizardPageDesignerActionList(WizardPageDesigner pageDesigner)
        : base(pageDesigner.Component)
      {
        wizardPageDesigner = pageDesigner;
        base.AutoShow = true;
      }

      public bool AllowCancel
      {
        get { return Page.AllowCancel; }
        set { SetProperty("AllowCancel", value); }
      }

      public bool AllowNext
      {
        get { return Page.AllowNext; }
        set { SetProperty("AllowNext", value); }
      }

      public bool IsFinishPage
      {
        get { return Page.IsFinishPage; }
        set { SetProperty("IsFinishPage", value); }
      }

      public WizardPage NextPage
      {
        get { return Page.NextPage; }
        set { SetProperty("NextPage", value); }
      }

      public bool ShowCancel
      {
        get { return Page.ShowCancel; }
        set { SetProperty("ShowCancel", value); }
      }

      public bool ShowNext
      {
        get { return Page.ShowNext; }
        set { SetProperty("ShowNext", value); }
      }

      private WizardPage Page
      {
        get { return Component as WizardPage; }
      }

      public override DesignerActionItemCollection GetSortedActionItems()
      {
        var col = new DesignerActionItemCollection();
        col.Add(new DesignerActionHeaderItem("Buttons"));
        col.Add(new DesignerActionPropertyItem("AllowCancel", "Cancel Button Enabled", "Buttons",
                                               "Enables the Cancel button when this page is shown."));
        col.Add(new DesignerActionPropertyItem("ShowCancel", "Cancel Button Visible", "Buttons"));
        col.Add(new DesignerActionPropertyItem("AllowNext", "Next Button Enabled", "Buttons"));
        col.Add(new DesignerActionPropertyItem("ShowNext", "Next Button Visible", "Buttons"));
        col.Add(new DesignerActionHeaderItem("Behavior"));
        col.Add(new DesignerActionPropertyItem("IsFinishPage", "Mark As Finish Page", "Behavior"));
        col.Add(new DesignerActionPropertyItem("NextPage", "Set Next Page:", "Behavior"));
        return col;
      }

      private void SetProperty(string propertyName, object value)
      {
        var property = TypeDescriptor.GetProperties(Page)[propertyName];
        if (property != null)
          property.SetValue(Page, value);
      }

      private void TestMeth()
      {
        MessageBox.Show("Hello");
      }
    }

    #endregion
  }
}